package com.admin.scroller.photoorvideo.fragment;

import android.arch.lifecycle.Observer;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import com.admin.scroller.photoorvideo.adapter.PhotosAdapter;
import com.admin.scroller.photoorvideo.beans.DatePhoto;
import com.admin.scroller.photoorvideo.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 列表 展示的 fragment
 */
public class PhotosFragment extends BasePhotoFragment {

    private PhotosAdapter adapter;

    private LinearLayoutManager layoutManager;
    private List<DatePhoto> datePhotos=new ArrayList<>();
    private int height=0;
    private boolean hasGetHigth=false;

    //记录列表每一item的高度
    private Map<Integer,Integer> mMapList=new HashMap<>();

    @Override
    void initData(){
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter=new PhotosAdapter(getActivity());
        recyclerView.setAdapter(adapter);
        if(viewModel!=null){
            viewModel.loadPhotos().observe(this, new Observer<List<DatePhoto>>() {
                @Override
                public void onChanged(@Nullable List<DatePhoto> localPhotos) {
                    Log.d("PhotosFragment","PhotosFragment   "+localPhotos.size());
                    datePhotos=localPhotos;
                    adapter.upData(localPhotos);
                    dataSize=datePhotos.size();
                    if(height==0) {
                        getAllHeight();
                    }
                }
            });
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        adapter.notifyDataSetChanged();
    }

    @Override
    void getAllHeight(){
        Point point=ScreenUtils.getScreenSize(getActivity());
        int w=point.x/4;
        height=w;
        int s=datePhotos.size();
        for(int i=1;i<s;i++){
            height+=90;
            int l=datePhotos.get(i).localPhotos.size()/4;
            if(datePhotos.get(i).localPhotos.size()-l*4>0){
                l+=1;
            }
            height+=l*w;
        }
        height-=point.y;
    }

    int iposition;
    int itemW;
    int itemH;
    @Override
    void getProgress() {
        layoutManager=(LinearLayoutManager) recyclerView.getLayoutManager();
        int itemWH = 0;
        int itemTR = 0;
        int distance = 0;
        int position = layoutManager.findFirstVisibleItemPosition();
        View firstVisiableChildView = layoutManager.findViewByPosition(position);
        //判断是横着还是竖着，得出宽或高
        itemWH = firstVisiableChildView.getHeight();
        //一层判断mMapList是否为空，若不为空则根据键判断保证不会重复存入
        if (mMapList.size() == 0) {
            mMapList.put(position, itemWH);
        } else {
            if (!mMapList.containsKey(position)) {
                mMapList.put(position, itemWH);
                Log.d("poi", mMapList + "");
            }
        }
        itemTR = firstVisiableChildView.getTop();
        //position为动态获取，目前屏幕Item位置
        for (int i = 0; i < position; i++) {
            //iposition移出屏幕的距离
            iposition = iposition + (mMapList.get(i)==null?0:mMapList.get(i));
        }
        //根据类型拿iposition减未移出Item部分距离，最后得出滑动距离
        distance = iposition - itemTR;
        //item宽高
        itemW = firstVisiableChildView.getWidth();
        itemH = firstVisiableChildView.getHeight();
        //归零
        iposition = 0;
        if(layoutManager.findLastVisibleItemPosition()==adapter.getItemCount()-1){
            if(height<distance){
                height=distance;
                hasGetHigth=true;
            }
        }
        progress=distance;
        if(!onTouchBar){
            int y=(int)(linearBody.getHeight()*progress*1f/height);
            setLinearBarLocation(y);
        }
    }

    @Override
    void changeTextDate(int index) {
        textDate.setText(((DatePhoto)datePhotos.get(index)).getDate());
    }


}
